package com.example.demo.Encrypt;

import org.springframework.util.DigestUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/***
 * 密码加密
 */
public class EncryptPwd {

    public static String SHA512(final String str){

        String Resultstr= null;
        if(str != null && str.length()>0) {

            try {
                MessageDigest messageDigest = MessageDigest.getInstance("SHA-512");
                messageDigest.update(str.getBytes());
                byte byteBuffer[] = messageDigest.digest();
                StringBuffer HexStr = new StringBuffer();

                for (int i = 0; i < byteBuffer.length; i++) {
                    String hex = Integer.toHexString(0xff & byteBuffer[i]);
                    if (hex.length() == 1) HexStr.append('0');
                    HexStr.append(hex);
                }
                Resultstr = HexStr.toString();

            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
        }
        return Resultstr;
    }
    // SHA-512加密

    public static String Encrypt(final String Pwd,final String salt) {

        String SHA512s = SHA512(Pwd);
        if(SHA512s==null) return null;

        String temp = SHA512s + salt;
        String md5str = DigestUtils.md5DigestAsHex(temp.getBytes());

        return md5str;
    }
    // SHA-512 加盐后套md5加密
}
